<?php
class Session{
	private $ID;
	private $Type;
	private $Licenses = array();
	static private $Session=false;
	static private function getInstance(){
		if (!isset($_SESSION['session'])){
			self::$Session =new Session();
			self::$Session->ID = -1;
			self::$Session->Type = 0 ;
			$_SESSION['session'] = serialize(self::$Session);
		}else{
			if(self::$Session instanceof Session) return;
			self::$Session = unserialize($_SESSION['session']);
		}
	}
	static function login($email_address,$password){
		if($password == "")return false;
		self::getInstance();
		$id = FunctionData::readAccountId($email_address);
		if($id>-1){
			$reg_data = FunctionData::readAccountRegisterInfo($email_address);
			if($reg_data->getPasswordHash() == UtilHash::sha256( $password ) ){
				self::$Session ->ID = $id;
				self::$Session ->Type = 1 ;
			}else{
			}
		}else{
		}
		$_SESSION['session'] = serialize(self::$Session);
		if(self::$Session->ID > -1){
			return true;
		}
		return false;
	}
	static function hardLogin($id){
		self::getInstance();
		if($id>-1){
			$type = FunctionData::getAccountTypeOfId($id);
			if($type != 0 ){
				{Error::getInstance() ->addError( 0 , "Hard Login id:".$id." type:".$type );} ;
				self::$Session ->Type = $type;
				self::$Session ->ID = $id;
			}else{
				{Error::getInstance() ->addError( 0 , "Hard Login id:".$id." type unknown" );} ;
			}
		}else{
			{Error::getInstance() ->addError( 24  , "Login with invalid account id" );} ;
			return false;
		}
		$_SESSION['session'] = serialize(self::$Session);
		if(self::$Session->ID > -1){
			return true;
		}
		return false;
	}
	static function getAccountType(){
		self::getInstance();
		return self::$Session->Type;
	}
	static function guestLogin($guestid){
		self::getInstance();
		$id = FunctionData::getGuestID($guestid);
		if($id > -1){
			self::$Session ->ID = $id;
			self::$Session ->Type = 2 ;
			$_SESSION['session'] = serialize(self::$Session);
			return true;
		}
		return false;
	}
	static function logout(){
		self::$Session ->License = array();
		self::$Session ->ID = -1;
		self::$Session ->Type = 0 ;
		$_SESSION['session'] = serialize(self::$Session);
	}
	static function getIdentify(){
		self::getInstance();
		return FunctionData::getIdentifyFromId(self::$Session ->ID);
	}
	static function getEmailAddress(){
		self::getInstance();
		return FunctionData::getEmailAddressFromId(self::$Session ->ID);
	}
	static function getRole($areaname, $group_name){
		$group_obj = FunctionData::readGroup($group_name);
		if($group_obj != null){
			return $group_obj->getLicense($areaname);
		}else{
			{Error::getInstance() ->addError( 12  , "error when get role" );} ;
		}
	}
	static function getRoleOfArea($areaname){
		self::getInstance();
		if(self::$Session ->Type == 2 ){
			return (-1) ;
		}
		if(is_array(self::$Session ->License)){
			if(isset(self::$Session ->License[$areaname])){
				return self::$Session ->License[$areaname];
			}
		}
		$user_email = FunctionData::getEmailAddressFromId(self::$Session ->ID);
		$join_groups = FunctionData::readUserGroups($user_email);
		$groups = $join_groups->getGroups();
		$license = (-1) ;
		if(is_array($groups)){
			foreach($groups as $key=>$group_name){
				$group_obj = FunctionData::readGroup($group_name);
				if($group_obj != null){
					$tmp = $group_obj->getLicense($areaname);
					if($tmp > $license){
						$license = $tmp;
					}
					if($license == (9) ){
						break;
					}
				}
			}
		}
		self::$Session ->License[$areaname] = $license;
		$_SESSION['session'] = serialize(self::$Session);
		return $license;
	}
	static function isLogin(){
		self::getInstance();
		if(self::$Session->ID>-1){
			return true;
		}
		return false;
	}
	static function getID(){
		return self::$Session ->ID;
	}
}
?>